package com.soft.servlet;

import com.soft.entity.Goods;
import com.soft.entity.Order;
import com.soft.entity.OrderInfo;
import com.soft.entity.UserInfo;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kongz
 * @version 1.0 2021/6/15 10:34
 * @description 订单详细信息
 */
@WebServlet(name = "OrderDetailServlet", urlPatterns = "/OrderDetail")
public class OrderDetailServlet extends HttpServlet {

    private Domain domain;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("method") != null) {
            String method = req.getParameter("method");
            if (method.equals("user")) UserOrderDetail(req, resp);
            else if (method.equals("admin")) AdminOrderDetail(req, resp);
        }
    }

    private void UserOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderXinXi(req);
        req.getRequestDispatcher("diandanxianxi.jsp").forward(req, resp);
    }

    private void AdminOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderXinXi(req);
        req.getRequestDispatcher("orderManageView.jsp").forward(req, resp);
    }

    private void OrderXinXi(HttpServletRequest req) {
        domain = new Domain();
        int orderId = Integer.parseInt(req.getParameter("orderId"));

        Order order = domain.getOrderById(orderId);
        UserInfo ui = domain.getUserInfo(order.getReceiver());
        List<OrderInfo> orderInfos = domain.getOrderInfoListByOrderId(orderId);
        req.setAttribute("orderInfos", orderInfos);
        req.setAttribute("order", order);
        req.setAttribute("receiver", ui);
        List<Goods> goods = new ArrayList<>();
        for (OrderInfo oi : orderInfos) {
            goods.add(domain.getGoodsById(oi.getGoodsId()));
        }
        req.setAttribute("goods", goods);
    }
}
